//
//  XLMineViewController.m
//  Mine
//
//  Created by 于鹏 on 2017/8/10.
//  Copyright © 2017年 YIVIEW. All rights reserved.
//

#import "XLMineViewController.h"
#import "XLMineBaseCellModel.h"
#import "XLMineHeadImageCellModel.h"
#import "XLMineInfoViewController.h"
#import "XLAccountManageViewController.h"
#import "XLMineSwitchViewController.h"
#import "XLAppLicenseViewController.h"
#import "XLVersionInfoViewController.h"

#import "XLUserInfoModel.h"
#import "YYModel.h"
//#import <RongIMKit/RCIM.h>
#import "RongIMKit/RCIM.h"
#import "XLUtilities.h"

@interface XLMineViewController ()
@property (nonatomic, strong)XLMineHeadImageCellModel *headModel;
@end

@implementation XLMineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    XLWeakSelf(self);
    
    //section0
//    UIImage *placeholderImage = [UIImage imageNamed:@"placeholderImage"];
   
   self.headModel = [[XLMineHeadImageCellModel alloc]initWithActionBlock:^(XLMineBaseCellModel *model, NSIndexPath *indexPath) {
       [weakself pushToMineInfoVC];
   }];
//    self.headModel.imageIcon = placeholderImage;
    NSString *portraitUrl = UserInfo.RYLogo;
    if ([portraitUrl isEqualToString:@""]) {
        portraitUrl = [XLUtilities defaultUserPortrait:[RCIM sharedRCIM].currentUserInfo];
    }
    [self.headModel setTitle:UserInfo.userName detailText:UserInfo.hospital imageUrl:portraitUrl];
    
    //section1
    NSArray *arr = @[@"账号管理"/*,@"同步管理"*/,@"消息管理",@"软件许可协议",@"版本信息"/*,@"中心地址配置"*/];
    NSMutableArray *section1 = [NSMutableArray array];
    for (int i = 0; i<arr.count; i++) {
        XLMineBaseCellModel *model = [[XLMineBaseCellModel alloc]initWithTitle:arr[i] actionBlock:^(XLMineBaseCellModel *model, NSIndexPath *indexPath) {
            switch (indexPath.row) {
                case 0:
                {
                    //账号管理
                    [self pushToViewController:[XLAccountManageViewController class]];
                    break;
                }
//                case 1:
//                {
//                    //同步管理
//                    XLMineSwitchViewController *viewController = [[XLMineSwitchViewController alloc]init];
//                    viewController.type = XLSwitchVCTypeSynch;
//                    [weakself.navigationController pushViewController:viewController animated:YES];
//                    break;
//                }
                case 1:
                {
                    //消息管理
                    XLMineSwitchViewController *viewController = [[XLMineSwitchViewController alloc]init];
                    viewController.type = XLSwitchVCTypeMessage;
                    [weakself.navigationController pushViewController:viewController animated:YES];
                    break;
                }
                case 2:
                {
                    //许可协议
                    [weakself pushToViewController:[XLAppLicenseViewController class]];
                    break;
                }
                case 3:
                {
                    //版本信息
                    [weakself pushToViewController:[XLVersionInfoViewController class]];
                    break;
                }
                    
                default:
                    break;
            }
        }];
        [section1 addObject:model];
    }
    [self.xl_dataSource addObject:@[self.headModel]];
    [self.xl_dataSource addObject:section1];
    [self.tableView reloadData];
}

- (void)pushToMineInfoVC {
    XLWeakSelf(self);
    XLMineInfoViewController *viewController = [[XLMineInfoViewController alloc]init];
    viewController.updateAvatarBlock = ^{
        [weakself initData];
    };
    [self.navigationController pushViewController:viewController animated:YES];

}
- (void)pushToViewController:(Class)class {
    XLBaseViewController *viewController = [[class alloc]init];
    [self.navigationController pushViewController:viewController animated:YES];
}

- (void)initData {
    [super initData];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:UserInfo.userID forKey:@"doctorID"];
    [parameters setValue:UserInfo.tokenCode forKey:@"token"];
    [XLHTTPRequest UM_getUserInfoWithParameters:parameters success:^(id responseObject) {
        XLUserInfoModel *model = [XLUserInfoModel yy_modelWithDictionary:responseObject[@"data"]];
        UserInfo.userID = model.doctorID;
        UserInfo.nickName = model.nickName;
        UserInfo.userName = model.userName;
        UserInfo.phone = model.phone;
        UserInfo.gender = model.gender;
        UserInfo.tokenCode = model.token;
        UserInfo.userLogo = model.head;
        UserInfo.hospital = model.hospitalName;
        UserInfo.doctorDepartment = model.doctorDepartment;
        UserInfo.RYID = model.ryID;
        UserInfo.RYName = model.userName;
        //更新用户信息
        RCUserInfo *userInfo = [RCIM sharedRCIM].currentUserInfo;
        if (![UserInfo.userLogo isEqualToString:@""]) {
            UserInfo.RYLogo = [UserInfo.serviceInfo.baseURL stringByAppendingString:UserInfo.userLogo];
            [RCIM sharedRCIM].currentUserInfo.portraitUri = UserInfo.RYLogo;
            userInfo.portraitUri = UserInfo.RYLogo;
        } else {
            UserInfo.RYLogo = @"";
        }
        userInfo.userId = UserInfo.RYID;
        userInfo.name = UserInfo.RYName;
        [[RCIM sharedRCIM] refreshUserInfoCache:userInfo withUserId:[RCIM sharedRCIM].currentUserInfo.userId];
        [UserInfo synchronize];
       
        //更新UI
        NSString *portraitUrl = UserInfo.RYLogo;
        if ([portraitUrl isEqualToString:@""]) {
            portraitUrl = [XLUtilities defaultUserPortrait:[RCIM sharedRCIM].currentUserInfo];
        }
        [self.headModel setTitle:UserInfo.userName detailText:UserInfo.hospital imageUrl:portraitUrl];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
        });
        
    } failure:^(NSError *error, NSString *info) {
        if (error == nil) {
            [self exitLogin:info];
        }
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
